'''
https://leetcode.cn/problems/high-five/description
'''
import heapq
from collections import defaultdict
from typing import List


class Solution:
    def highFive(self, items: List[List[int]]) -> List[List[int]]:
        id_scores = defaultdict(list)
        for item in items:
            scores = id_scores[item[0]]
            heapq.heappush(scores, item[1])
            if len(scores) > 5:
                heapq.heappop(scores)
        res = []
        for id, scores in id_scores.items():
            res.append([id, sum(scores) // len(scores)])
        return sorted(res, key=lambda x: x[0])
